package com.moleq.logic;

import java.util.Date;
import java.util.List;

import com.moleq.device.DeviceManager;
import com.moleq.device.printer.Printer;
import com.moleq.device.printer.PrinterBufferColumn;
import com.moleq.device.printer.PrinterBufferRow;
import com.moleq.device.printer.PrinterBufferRowColection;
import com.moleq.enums.ItemType;
import com.moleq.enums.PaymentType;
import com.moleq.enums.TransactionType;
import com.moleq.models.GlobalInfo;
import com.moleq.models.PosTender;
import com.moleq.models.Receipt;
import com.moleq.models.SaleListItem;
import com.moleq.service.ServiceManager;
import com.moleq.util.BigDecmialTool;
import com.moleq.util.MoleQUtil;
import com.moleq.util.StringUtils;

public class ReceiptManager {

	


	public static PrinterBufferRowColection getNoSaleReceipt(String cashier,TransactionManager trnMgr) {

//		PrinterBufferRow row = new PrinterBufferRow();
//		PrinterBufferColumn column = new PrinterBufferColumn();

		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();

		// Header
		PrinterBufferRowColection headerBuffer =  getHeaderBuffer();
		if (headerBuffer != null) {
			for (int i = 0; i < headerBuffer.count(); i++) {
				printerBuffer.add(headerBuffer.get(i));
			}
		}
				
		printerBuffer.addFullRow("=");//===============================
		// pos information
		PrinterBufferRowColection posInfoBuffer =  getPosInfoBuffer(trnMgr.TransactionNo, trnMgr.dDoneDate, cashier);
		if (posInfoBuffer != null) {
			for (int i = 0; i < posInfoBuffer.count(); i++) {
				printerBuffer.add(posInfoBuffer.get(i));
			}
		}

		printerBuffer.addNormalRow("*********************************",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
		printerBuffer.addNormalRow("*****  No Sale Transaction  *****",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
		printerBuffer.addNormalRow("*********************************",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
		printerBuffer.addFullRow("=");//===============================

		// footer
		PrinterBufferRowColection footerBuffer =  getFooterBuffer();
		if (footerBuffer != null) {
			for (int i = 0; i < footerBuffer.count(); i++) {
				printerBuffer.add(footerBuffer.get(i));
			}
		}
		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getPosInfoBuffer(int trnNo, Date dDoneDate, String cashier) {
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		printerBuffer.addNormalRow("Station " + String.format("%02d", GlobalInfo.getInstance().stationNo) + "  " + String.format("%06d", trnNo),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow(MoleQUtil.getDateToString("yyyy-MM-dd", dDoneDate) + " " + MoleQUtil.getDateToString("HH:mm:ss", dDoneDate),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("Cashier # " + GlobalInfo.getInstance().cashier,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addBlankRow();
		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getHeaderBuffer() {
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		printerBuffer.addBlankRow();
		PrinterBufferRow row = new PrinterBufferRow();
		PrinterBufferColumn column = new PrinterBufferColumn();
		List<Receipt> hearderlst = ServiceManager.getInstance().mReceiptService.FindALLHeader();
		if (hearderlst != null) {
			for (int k = 0; k < hearderlst.size(); k++) {
				int align = 0, fontSize = 10;

				if (hearderlst.get(k).getLarge().equals("L")) {
					fontSize = PosPrinter.FONT_DOUBLE_SIZE;
				} else if (hearderlst.get(k).getLarge().equals("N")) {
					fontSize = PosPrinter.FONT_SINGLE_SIZE;
				}

				if (hearderlst.get(k).getJustify().equals("L")) {
					align = PosPrinter.LEFT;
				} else if (hearderlst.get(k).getJustify().equals("C")) {
					align = PosPrinter.CENTER;
				} else if (hearderlst.get(k).getJustify().equals("R")) {
					align = PosPrinter.RIGHT;
				}
				
				row = new PrinterBufferRow();
				column = new PrinterBufferColumn();
				column.setScale(100);
				column.setContent(hearderlst.get(k).getMsg());
				column.setFontSize(fontSize);
				column.setAlign(align);
				row.columns.add(column);
				printerBuffer.add(row);

			}
		}
		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getFooterBuffer(){
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		PrinterBufferRow row = new PrinterBufferRow();
		PrinterBufferColumn column = new PrinterBufferColumn();
		List<Receipt> footerlst = ServiceManager.getInstance().mReceiptService.FindALLTrailer();
		if (footerlst != null) {
			for (int k = 0; k < footerlst.size(); k++) {
				int align = 0, fontSize = 10;

				if (footerlst.get(k).getLarge().equals("L")) {
					fontSize = PosPrinter.FONT_DOUBLE_SIZE;
				} else if (footerlst.get(k).getLarge().equals("N")) {
					fontSize = PosPrinter.FONT_SINGLE_SIZE;
				}

				if (footerlst.get(k).getJustify().equals("L")) {
					align = PosPrinter.LEFT;
				} else if (footerlst.get(k).getJustify().equals("C")) {
					align = PosPrinter.CENTER;
				} else if (footerlst.get(k).getJustify().equals("R")) {
					align = PosPrinter.RIGHT;
				}
				
				row = new PrinterBufferRow();
				column = new PrinterBufferColumn();
				column.setScale(100);
				column.setContent(footerlst.get(k).getMsg());
				column.setFontSize(fontSize);
				column.setAlign(align);
				row.columns.add(column);
				printerBuffer.add(row);
			
			}
		}
		return printerBuffer;
	}

	public static PrinterBufferRowColection getSaleReceipt(String cashier, TransactionManager trnMgr) {
		
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		
		String value1 = "";
		String sPrice = "";

		// Header
		PrinterBufferRowColection headerBuffer =  getHeaderBuffer();
		if (headerBuffer != null) {
			for (int i = 0; i < headerBuffer.count(); i++) {
				printerBuffer.add(headerBuffer.get(i));
			}
		}
		
		printerBuffer.addFullRow("=");
		if (trnMgr.isReprint) {
			printerBuffer.addNormalRow("*** REPRINT ***",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		}
		// pos information
		PrinterBufferRowColection posInfoBuffer =  getPosInfoBuffer(trnMgr.TransactionNo, trnMgr.dDoneDate, cashier);
		if (posInfoBuffer != null) {
			for (int i = 0; i < posInfoBuffer.count(); i++) {
				printerBuffer.add(posInfoBuffer.get(i));
			}
		}

		if (trnMgr.TransType == TransactionType.RETURN) {
			printerBuffer.addNormalRow("---------");
			printerBuffer.addNormalRow("Refund");
			printerBuffer.addNormalRow("---------");
			printerBuffer.addBlankRow();
		}

		
		// sale list detail
		PrinterBufferRowColection tmpBuffer =  getSaleDetailBuffer(trnMgr);
		if (tmpBuffer != null) {
			for (int i = 0; i < tmpBuffer.count(); i++) {
				printerBuffer.add(tmpBuffer.get(i));
			}
		}
		
		printerBuffer.addFullRow("-");
		
//		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.mSaleListItems.getItemCountForReceipt()) + " ";
		sPrice = "  " + trnMgr.mSaleListItems.getItemCountForReceipt() + "  ";
		printerBuffer.addTenderItem("Item Count:", sPrice);
		
		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getSubTotal() + trnMgr.getTransDiscount() ) + " ";
		printerBuffer.addTenderItem( "Subtotal:", sPrice);

		if (trnMgr.getTransDiscount() != 0) {
			sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getTransDiscount()) + " ";
			printerBuffer.addTenderItem( trnMgr.TransDntValue + "% Discount:", sPrice);
		}

		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getCurrentTax()) + " ";
		printerBuffer.addTenderItem( "Tax:", sPrice);


		double TaxExempat = 0;
		TaxExempat = trnMgr.getTotalTaxExem() * -1;

		if (BigDecmialTool.round(TaxExempat, 2) < 0) {
			sPrice = "  " + MoleQUtil.fixPrintPrice(TaxExempat) + " ";
			printerBuffer.addTenderItem( "Tax Exempt:", sPrice);
		}

		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getAmountPayable()) + " ";
		printerBuffer.addTenderItem("Total:", sPrice);

		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getAmountOfPaidUp()) + " ";
		printerBuffer.addTenderItem( "Tender:", sPrice);

		sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getChange()) + " ";
		printerBuffer.addTenderItem( "Change:", sPrice);

		if (trnMgr.tenderList.size() > 0) {
//			printerBuffer.addNormalRow("--------------  Tender Detail  ---------------");
			printerBuffer.addNormalRow("---------  Tender Detail  ---------");
			for (int i = 0; i < trnMgr.tenderList.size(); i++) {
				
				value1 = trnMgr.tenderList.get(i).paymentType.getText().toString() + ":";
				sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.tenderList.get(i).getPt_tender()) + " ";
				printerBuffer.addTenderItem( value1, sPrice);

				String sAuth =  trnMgr.tenderList.get(i).getPt_Auth();
				if (sAuth != null && !sAuth.equals("")) {
					value1 = "";
					if (trnMgr.tenderList.get(i).paymentType == PaymentType.CHECK) {
						value1 = "Check #:";
					} else {
						value1 = "Auth #:";
					}

					sPrice = "  " + sAuth + " ";
					printerBuffer.addTenderItem( value1, sPrice);
				}

			}

		}
		printerBuffer.addFullRow("=");
		
		// footer
		PrinterBufferRowColection footerBuffer =  getFooterBuffer();
		if (footerBuffer != null) {
			for (int i = 0; i < footerBuffer.count(); i++) {
				printerBuffer.add(footerBuffer.get(i));
			}
		}

		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getServiceDetailContent(List<SaleListItem> listItem) {
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		int iTotalLen = 42;
		int iP4Len = 14;
		int iQtyLen = 6;
		int iContentLen = iTotalLen - iP4Len - iQtyLen;
		String sLine = "";
		int iLen = 0;
		for (int i = 0; i < listItem.size(); i++) {
			sLine = "";

			SaleListItem saleitem = listItem.get(i);
			iLen = 0;
			String sQty = String.valueOf(saleitem.getQty());
			iLen = sQty.length();
			sLine += sQty + StringUtils.getString(" ", iQtyLen- iLen);
			String sName = saleitem.getName();
			iLen = sLine.length();
			
			if (sName.length() + iLen > iContentLen) {
				
				sName = sName.substring(0, iContentLen - iLen);
				sLine += sName;
			} else {
				iLen = iContentLen - (sName.length() + iLen);
				sLine += sName + StringUtils.getString(" ", iLen);
			}
			printerBuffer.addNormalRow(sLine,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		}

		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getServiceTicketLocalReceipt(String cashier, TransactionManager trnMgr,List<SaleListItem> listItem) {
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		
		if (trnMgr.haveServiceItem()) {
			// Header
			printerBuffer.addBlankRow();
			printerBuffer.addBlankRow();
			PrinterBufferRowColection headerBuffer =  getHeaderBuffer();
			if (headerBuffer != null) {
				for (int i = 0; i < headerBuffer.count(); i++) {
					printerBuffer.add(headerBuffer.get(i));
				}
			}
			printerBuffer.addFullRow("=");// ==============================
			if (trnMgr.isReprint) {
				printerBuffer.addNormalRow("*** REPRINT ***",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			}
			printerBuffer.addNormalRow("Station " + String.format("%02d", GlobalInfo.getInstance().stationNo) + "  " + String.format("%06d", trnMgr.TransactionNo),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addNormalRow(MoleQUtil.getDateToString("yyyy-MM-dd", trnMgr.dDoneDate) + " " + MoleQUtil.getDateToString("HH:mm:ss", trnMgr.dDoneDate),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addBlankRow();
			
			printerBuffer.addNormalRow("--------------------",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addNormalRow("SERVICE TICKET",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addNormalRow("--------------------",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addBlankRow();
			printerBuffer.addNormalRow("SERVICE # " + String.format("%02d", GlobalInfo.getInstance().stationNo) + trnMgr.TransactionNo,Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addBlankRow();
			
			//service item detail
			PrinterBufferRowColection detailBuffer = getServiceDetailContent(listItem);
			if (detailBuffer != null) {
				for (int i = 0; i < detailBuffer.count(); i++) {
					printerBuffer.add(detailBuffer.get(i));
				}
			}
			
			printerBuffer.addFullRow("-");
			printerBuffer.addNormalRow("PLEASE USE THIS TICKET TO PICKUP YOUR",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addNormalRow("ITEMS IN THE SERVICE DEPARTMENT",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addFullRow("=");
			// footer
			PrinterBufferRowColection footerBuffer =  getFooterBuffer();
			if (footerBuffer != null) {
				for (int i = 0; i < footerBuffer.count(); i++) {
					printerBuffer.add(footerBuffer.get(i));
				}
			}
		}
		return printerBuffer;
	}
	
	public static PrinterBufferRowColection getServiceItemReceipt(String cashier, TransactionManager trnMgr,List<SaleListItem> listItem) {
		
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		if (trnMgr.haveServiceItem()) {
			printerBuffer.addBlankRow();
			printerBuffer.addBlankRow();
			printerBuffer.addNormalRow("Service  Ticket   " + String.format("%02d", GlobalInfo.getInstance().stationNo) + trnMgr.TransactionNo,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			// pos information
			printerBuffer.addNormalRow("Station " + String.format("%02d", GlobalInfo.getInstance().stationNo) + "  " + String.format("%06d", trnMgr.TransactionNo),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addNormalRow(MoleQUtil.getDateToString("yyyy-MM-dd", trnMgr.dDoneDate) + " " + MoleQUtil.getDateToString("HH:mm:ss", trnMgr.dDoneDate),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			printerBuffer.addBlankRow();
			
//		printerBuffer.addBlankRow();
//		printerBuffer.addNormalRow("---------------------",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
//		printerBuffer.addNormalRow("SERVICE TICKET",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
//		printerBuffer.addNormalRow("---------------------",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
//		printerBuffer.addNormalRow("SERVICE # " + String.format("%02d", GlobalInfo.getInstance().stationNo) + trnMgr.TransactionNo,
//				Printer.FONT_SINGLE_SIZE,Printer.CENTER);
//		printerBuffer.addBlankRow();
			
			printerBuffer.addFullRow("=");
			// sale list detail
			PrinterBufferRowColection tmpBuffer = getServiceDetailContent(listItem);
			if (tmpBuffer != null) {
				for (int i = 0; i < tmpBuffer.count(); i++) {
					printerBuffer.add(tmpBuffer.get(i));
				}
			}
			printerBuffer.addBlankRow();
			printerBuffer.addFullRow("=");
		}
		return printerBuffer;
	}
	
	// cancel transaction receipt
	public static PrinterBufferRowColection getCancelTrnReceipt(String cashier,TransactionManager trnMgr) {
		
			String sPrice = "";
			
			PrinterBufferRow row = new PrinterBufferRow();
			PrinterBufferColumn column = new PrinterBufferColumn();
			PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();

			// Header
			PrinterBufferRowColection headerBuffer =  getHeaderBuffer();
			if (headerBuffer != null) {
				for (int i = 0; i < headerBuffer.count(); i++) {
					printerBuffer.add(headerBuffer.get(i));
				}
			}
			printerBuffer.addFullRow("=");// ==============================
			
			// pos information
			PrinterBufferRowColection posInfoBuffer =  getPosInfoBuffer(trnMgr.TransactionNo, trnMgr.dDoneDate, cashier);
			if (posInfoBuffer != null) {
				for (int i = 0; i < posInfoBuffer.count(); i++) {
					printerBuffer.add(posInfoBuffer.get(i));
				}
			}

			// sale list detail
			PrinterBufferRowColection tmpBuffer =  getSaleDetailBuffer(trnMgr);
			if (tmpBuffer != null) {
				for (int i = 0; i < tmpBuffer.count(); i++) {
					printerBuffer.add(tmpBuffer.get(i));
				}
			}
			
			printerBuffer.addBlankRow();   // add blank row
			printerBuffer.addFullRow("-"); // -----------------------------------------------

			/******************************* change *****************************/
			sPrice = "  " + trnMgr.mSaleListItems.getItemCountForReceipt() + "  ";
			printerBuffer.addTenderItem("Item Count:",sPrice);
			/*********************************************************************/


			// Subtotal
			sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getAmountPayable()) + " ";
			row = new PrinterBufferRow();
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent("Subtotal:");
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent(sPrice);
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			printerBuffer.add(row);
			
			// transaction discount 
			if (trnMgr.getTransDiscount() != 0) {
				sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getTransDiscount()) + " ";
				row = new PrinterBufferRow();
				column = new PrinterBufferColumn();
				column.setScale(40);
				column.setContent(trnMgr.TransDntValue + "% Discount:");
				column.setAlign(Printer.RIGHT);
				row.columns.add(column);
				
				column = new PrinterBufferColumn();
				column.setScale(40);
				column.setContent(sPrice);
				column.setAlign(Printer.RIGHT);
				row.columns.add(column);
				
				printerBuffer.add(row);
			}
			
			// tax
			sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getCurrentTax()) + " ";
			row = new PrinterBufferRow();
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent("Tax:");
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent(sPrice);
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			printerBuffer.add(row);


			// get manual taxexmpat
			double TaxExempat = 0;

			TaxExempat = trnMgr.getTotalTaxExem() * -1;

			if (BigDecmialTool.round(TaxExempat, 2) < 0) {
				sPrice = "  " + MoleQUtil.fixPrintPrice(TaxExempat) + " ";
				
				row = new PrinterBufferRow();
				column = new PrinterBufferColumn();
				column.setScale(40);
				column.setContent("Tax Exempt:");
				column.setAlign(Printer.RIGHT);
				row.columns.add(column);
				
				column = new PrinterBufferColumn();
				column.setScale(40);
				column.setContent(sPrice);
				column.setAlign(Printer.RIGHT);
				row.columns.add(column);
				
				printerBuffer.add(row);
			}

			sPrice = "  " + MoleQUtil.fixPrintPrice(trnMgr.getSubTotal()) + " ";
			row = new PrinterBufferRow();
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent("Total:");
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			column = new PrinterBufferColumn();
			column.setScale(40);
			column.setContent(sPrice);
			column.setAlign(Printer.RIGHT);
			row.columns.add(column);
			
			printerBuffer.add(row);
		
			printerBuffer.addNormalRow("***********************************",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addNormalRow("***            VOID             ***",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addNormalRow("***    Transaction Canceled     ***",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addNormalRow("***********************************",Printer.FONT_SINGLE_SIZE,Printer.CENTER);
			printerBuffer.addFullRow("=");// ===============================================
			
			// footer
			PrinterBufferRowColection footerBuffer =  getFooterBuffer();
			if (footerBuffer != null) {
				for (int i = 0; i < footerBuffer.count(); i++) {
					printerBuffer.add(footerBuffer.get(i));
				}
			}

			return printerBuffer;
		}
	
	public static PrinterBufferRowColection getSaleDetailBuffer(TransactionManager trnMgr){
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
		String description = "";
		int iTotalLen = 42;
		int iP4Len = 14;
		int iContentLen = iTotalLen - iP4Len;
		String sLine = "";
		String sPrice = "";
		int iLen = 0;
		boolean showTax = false;
		boolean showFootstamp = false;
		for (int i = 0; i < trnMgr.mSaleListItems.getItemCount(); i++) {
			sLine = "";
			SaleListItem saleitem = trnMgr.mSaleListItems.getItem(i);
			String sName = saleitem.getName();
			if (saleitem.getItemType() == ItemType.PACKAGE_PRICE_FIX || saleitem.getItemType() == ItemType.LIMITED_QTY_PRICE_FIX
				||	saleitem.getItemType() == ItemType.DISCOUNT_AMOUNT || saleitem.getItemType() == ItemType.DISCOUNT_PERECENT
				||	saleitem.getItemType() == ItemType.DISCOUNT_LINK) {
				sName = trnMgr.mSaleListItems.getItem(i).getDisplayContent();
			}
			
			if (saleitem.isRefund()) {
				if (saleitem.getItemType() == ItemType.REGULAR || saleitem.getItemType() == ItemType.OPEN_DEPARTMENT || 
						saleitem.getItemType() == ItemType.PACKAGE_PRICE) {
					sName = sName + " (Refund)";
				}
			}
			
			iLen = sLine.length();
			if (sName.length() + iLen > iContentLen) {

				sName = sName.substring(0, iContentLen - iLen);
				sLine += sName;
			} else {
				iLen = iContentLen - (sName.length() + iLen);
				sLine += sName + StringUtils.getString(" ", iLen);
			}
			
			showTax = false;
			showFootstamp = false;
			if (saleitem.getTax1() || saleitem.getTax2() || saleitem.getTax3() || saleitem.getTax4() || saleitem.getTax5() || saleitem.getTax6()) {
				showTax = true;
			}

			if (saleitem.getFoodstamp()) {
				showFootstamp = true;
			}

			// price
			sPrice = "  " + MoleQUtil.fixPrintPrice(saleitem.getPrice(), showFootstamp, showTax);
			
			if (!saleitem.getEachDescription().equals("")) {
				description = saleitem.getEachDescription();
				description = getDescription(description,sPrice);
				
				printerBuffer.addNormalRow(description,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			}else if (!saleitem.getPriceDescription().equals("")) {
				description = saleitem.getPriceDescription() + saleitem.getmTareDesc();
				description = getDescription(description, sPrice);
				
				printerBuffer.addNormalRow(description,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			}else if (!saleitem.getDepDescription().equals("")) {
				description = saleitem.getDepDescription() + saleitem.getmTareDesc();
				description = getDescription(description,sPrice);
				
				printerBuffer.addNormalRow(description,Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			}else{
				iLen = iP4Len - sPrice.length();
				if (iLen == 0) {
					sLine += sPrice;
				} else {
					sLine += StringUtils.getString(" ", iLen) + sPrice;
				}
			}
			
//			printerBuffer.addSaleItem(sName,sPrice);
			printerBuffer.addNormalRow(sLine,Printer.FONT_SINGLE_SIZE,Printer.LEFT);

			if (GlobalInfo.getInstance().isShowDesc && !saleitem.getDescription().equals("")) {
				printerBuffer.addNormalRow(saleitem.getDescription(),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
			}
		}
		return printerBuffer;
	}
	
	public static String getDescription(String description,String sPrice){
		
		int iTotalLen = 42;
		int iP4Len = 14;
		int iContentLen = iTotalLen - iP4Len;
		
		int iLen = 0;
		if (description.length() + iLen > iContentLen) {

			description = description.substring(0, iContentLen - iLen);
		} else {
			iLen = iContentLen - (description.length() + iLen);
			description = description + StringUtils.getString(" ", iLen);
		}
		
		iLen = iP4Len - sPrice.length();
		if (iLen == 0) {
			description += sPrice;
		} else {
			description += StringUtils.getString(" ", iLen) + sPrice;

		}
		
		return description;
	}
	
//	public static PrinterBufferRowColection getServiceItemBuffer(List<SaleListItem> listItem){
//		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();
//		for (int i = 0; i < listItem.size(); i++) {
//			String sQty = String.valueOf(listItem.get(i).getQty());
//			
//			String sName = listItem.get(i).getName();
//
//			printerBuffer.addServiceItem(sQty,sName);
//		}
//		return printerBuffer;
//	}

	public static PrinterBufferRowColection getSignature(int transNo,Date dDoneDate,String cashier,PosTender tender,boolean isReprint){
		PrinterBufferRowColection printerBuffer = new PrinterBufferRowColection();

		// Header
		PrinterBufferRowColection headerBuffer =  getHeaderBuffer();
		if (headerBuffer != null) {
			for (int i = 0; i < headerBuffer.count(); i++) {
				printerBuffer.add(headerBuffer.get(i));
			}
		}
				
		printerBuffer.addFullRow("=");//===============================
		
		if (isReprint) {
			printerBuffer.addNormalRow("*** REPRINT ***",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		}
		// pos information
		PrinterBufferRowColection posInfoBuffer =  getPosInfoBuffer(transNo, dDoneDate, cashier);
		if (posInfoBuffer != null) {
			for (int i = 0; i < posInfoBuffer.count(); i++) {
				printerBuffer.add(posInfoBuffer.get(i));
			}
		}
		
		printerBuffer.addBlankRow();
		printerBuffer.addNormalRow("CREDIT CARD PAYMENT       " + MoleQUtil.fixPrintPrice(tender.getPt_tender()),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		
		if (!tender.getPt_card_no().equals("")) {
			printerBuffer.addNormalRow(tender.getPt_card_no() + "   " + tender.getPt_card_type());
		}
		printerBuffer.addNormalRow("AUTH     " + tender.getPt_Auth(),Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addBlankRow();
		printerBuffer.addNormalRow("CARD HOLDER ACKNOWLENDGES RECEIPT OF",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("GOODS/SERVICES EQUAL TO THE AMOUNT SET",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("FORTH ABOVE AND AGREES TO ABIDE BY THE",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("ISSUERS AGREEMENT WITH THE CARDHOLDER",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addBlankRow();
		printerBuffer.addBlankRow();
		printerBuffer.addBlankRow();
		printerBuffer.addBlankRow();
		printerBuffer.addNormalRow("X _____________________________________________",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("                  SIGNATURE                    ",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addNormalRow("                                               ",Printer.FONT_SINGLE_SIZE,Printer.LEFT);
		printerBuffer.addFullRow("=");//===============================

		// footer
		PrinterBufferRowColection footerBuffer =  getFooterBuffer();
		if (footerBuffer != null) {
			for (int i = 0; i < footerBuffer.count(); i++) {
				printerBuffer.add(footerBuffer.get(i));
			}
		}
		return printerBuffer;
	}
	
	
//	public static void printSignaturePaper( final PosTender mCurrentTender) {
//		final PosPrinter printer = (PosPrinter) DeviceManager.getInstance().printer;
//		final TransactionManager mTransMgr = TransactionManager.getInstance();
//		new Thread(new Runnable() {
//			public void run() {
//
//				String signatureContent = "";
//
//				if (printer.getEnable() && !printer.isBusy()) {
//					printer.onConnect();
//					signatureContent = mTransMgr.getCreditContent(mCurrentTender);
//					printer.print(signatureContent);
//					printer.cutPaper();
//					printer.onDisconnect();
//
//				}
//			}
//		}).start();
//		
//	}
	

}
